//
//  HMStatusView.swift
//  WeiBo14
//
//  Created by LCH on 16/3/9.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
import SnapKit


class HMStatusView: UIView {
    
    var bottomConstraint: Constraint?
    
    var statusViewModel:HMStatusViewModel? {
    
        didSet {
        
            if let urlString = statusViewModel?.status?.user?.profile_image_url, let url = NSURL(string:urlString){
            avater.sd_setImageWithURL(url,placeholderImage: UIImage(named: "avatar_default"))
                
            }
            
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            timeLabel.text = statusViewModel?.status?.created_at
            messageLabel.text = statusViewModel?.status?.source
            avatar_vip.image = statusViewModel?.verifiedImage
            vipImage.image = statusViewModel?.membershipImage
            contextLabel.text = statusViewModel?.status?.text
           
            
            self.bottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
            
             // 有配图
            //1. 设置数据
                
            picture.picUrls = picUrls
            // 2. 显示配图视图的控件
                picture.hidden = false
            // 3.更改原创微博的底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(picture).offset(Margin).constraint
                })
                
            }else {
            
             picture.hidden = true
             
               self.snp_makeConstraints(closure: { (make) -> Void in
                
                self.bottomConstraint = make.bottom.equalTo(contextLabel).offset(Margin).constraint
               })
            
            }
            
           
        }
    
    
    
    }

   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 //TODO: 高度写死为什么设置颜色无法显示
    private func setupUI() {
        
//        backgroundColor = RGB(r : CGFloat(random()%256), g: CGFloat(random()%256), b: CGFloat(random()%256))
//        backgroundColor = UIColor.redColor()
        backgroundColor = UIColor.whiteColor()
        
    addSubview(avater)
    addSubview(nameLabel)
    addSubview(timeLabel)
    addSubview(messageLabel)
    addSubview(avatar_vip)
    addSubview(vipImage)
    addSubview(contextLabel)
    addSubview(picture)
        
        
   // 头像
    avater.snp_makeConstraints { (make) -> Void in
        
        make.top.leading.equalTo(Margin)
        make.width.equalTo(AvaterSize)
        make.height.equalTo(AvaterSize)
        
        }
    // 微博名
    nameLabel.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(avater.snp_top)
        make.leading.equalTo(avater.snp_trailing).offset(Margin)
        
        }
    // 发布时间
    timeLabel.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(nameLabel.snp_bottom)
        make.leading.equalTo(nameLabel.snp_leading)
        
        }
     // vip
     vipImage.snp_makeConstraints { (make) -> Void in
        
          make.leading.equalTo(nameLabel.snp_trailing).offset(Margin)
          make.top.equalTo(nameLabel.snp_top)
        }
     // 微博认证
     avatar_vip.snp_makeConstraints { (make) -> Void in
        
         make.centerX.equalTo(avater.snp_trailing).offset(-3)
         make.centerY.equalTo(avater.snp_bottom).offset(-3)
        
        
        }
    // 消息来源
     messageLabel.snp_makeConstraints { (make) -> Void in
        
          make.leading.equalTo(timeLabel.snp_trailing).offset(Margin)
          make.top.equalTo(timeLabel.snp_top)
        
        }
        
    // 内容约束
        contextLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(avater)
            make.top.equalTo(avater.snp_bottom).offset(Margin)
            make.trailing.equalTo(self).offset(-Margin)
            
        }
    //TODO:这个约束的作用
        self.snp_makeConstraints { (make) -> Void in
            
//            make.bottom.equalTo(contextLabel).offset(Margin)
            
            self.bottomConstraint = make.bottom.equalTo(picture).offset(Margin).constraint
        }
        
    // 微博配图
        
        picture.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contextLabel)
            make.top.equalTo(contextLabel.snp_bottom).offset(Margin)
            
            make.size.equalTo(CGSizeMake(100, 100))
        }
    
    }
    
    
    
    
    
    
    
   // MARK: - 懒加载控件
    // 头像
    private lazy var avater: UIImageView = {
    
     let img = UIImageView(image: UIImage(named: "avatar_default"))
        
      return img
    
    }()
    // 名字
    private lazy var nameLabel: UILabel = {
    
      let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
      label.text = "hehehehhe"
      
      return label
    
    }()
    // 微博发布时间
    private lazy var timeLabel: UILabel = {
    
    let label = UILabel(textColor:  UIColor.redColor(), fontSize: 12)
    label.text = "hahahahhahahah"
    
     return label
    }()
    
   // VIP
    private lazy var vipImage: UIImageView = {
    
    let vip = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    return vip
    }()
    
    //微博认证图标
    private lazy var avatar_vip: UIImageView = {
    
    let avatarVip = UIImageView(image: UIImage(named: "avatar_vip"))
        
     return avatarVip
    
    }()
    
    
    // 消息来源
    private lazy var messageLabel: UILabel = {
    
    let messLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 12)
     messLabel.text = "来源自 我的分享"
    return messLabel
    }()
    
    private lazy var contextLabel: UILabel = {
    
     let conLabel = UILabel(textColor: UIColor.darkGrayColor() , fontSize: 15)
      
    conLabel.numberOfLines = 0
    
     return conLabel
    }()
    
    // 微博配图 
    
    private lazy var picture:HMStatusPictureView = HMStatusPictureView(frame: CGRectZero,collectionViewLayout:UICollectionViewFlowLayout())

}
